package com.toycloud.system.utils;

import com.alibaba.fastjson2.JSON;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
public class OnesUtil {
    private static final String HOST = "https://ones.toycloud.com";
    private static final String AUTH_LOGIN = "/project/api/project/auth/login";
    private static final String QUERY_TASK_DETAIL = "/project/api/project/team/:teamUUID/task/:taskUUIDOrNumber/info";
    private static final String TASK_STATUSES = "/project/api/project/team/:teamUUID/task_statuses";

    public static AuthData authLogin(String email, String password) {
        try {
            String url = HOST + AUTH_LOGIN;
            String jsonInput = String.format("{\"email\":\"%s\", \"password\":\"%s\"}", email, password);
            Map<String, String> headers = new HashMap<>();
            headers.put("Content-Type", "application/json");
            // 使用 HttpUtils 调用 POST 请求
            String response = HttpUtils.sendPost(url, jsonInput, headers);
            AuthData authData = JSON.parseObject(response, AuthData.class);
            return authData;
        } catch (Exception e) {
            log.error("OnesUtil.authLogin:"+e.getMessage());;
            return null;
        }
    }
    
    private static Map<String, String> getHeaders() {
//        AuthData authData = authLogin("", "");
        Map<String, String> headers = new HashMap<>();
        headers.put("Ones-User-Id", "XtxsBGu9"); //authData.getUser().uuid
        headers.put("Ones-Auth-Token", "AG56bncPW50uqdtEZJkFDL4YjPUIpsB0JKkhMwZEiX1xfrGpfvghTM6s9dkS8bpe"); //authData.getUser().token
        headers.put("Referer", HOST);
        headers.put("Content-Type", "application/json");
        return headers;
    }

    /**
     * 获取工作项详情
     *
     * @param teamUUID
     * @param taskUUIDOrNmuber
     * @return
     */
    public static TaskDetail getTaskDetail(String teamUUID, String taskUUIDOrNmuber) {
        try {
            // 替换URL中的占位符
            String url = HOST + QUERY_TASK_DETAIL
                    .replace(":teamUUID", teamUUID)
                    .replace(":taskUUIDOrNumber", taskUUIDOrNmuber);

            // 调用GET请求
            String response = HttpUtils.sendGet(url, getHeaders());

            // 假设需要对响应进行处理
            TaskDetail taskDetail = JSON.parseObject(response, TaskDetail.class);
            return taskDetail;
        } catch (Exception e) {
            log.error("OnesUtil.getTaskDetail:"+e.getMessage());;
            return null;
        }
    }

    /**
     * 获取状态列表
     *
     * @param teamUUID
     * @return
     */
    public static TaskStatuses getTaskStatuses(String teamUUID) {
        try {
            String url = HOST + TASK_STATUSES
                    .replace(":teamUUID", teamUUID);

            String response = HttpUtils.sendGet(url, getHeaders());
            TaskStatuses taskStatuses = JSON.parseObject(response, TaskStatuses.class);
            return taskStatuses;
        } catch (Exception e) {
            log.error("OnesUtil.getTaskStatuses:"+e.getMessage());;
            return null;
        }
    }

    public static void main(String[] args) {
//        System.out.printf(JSON.toJSONString(getTaskDetail("QspnyQrR", "64876")));
//        System.out.printf(JSON.toJSONString(getTaskStatuses("QspnyQrR")));
    }

    @Data
    public static class AuthData {
        private User user;
    }

    @Data
    public static class User {
        private String uuid;
        private String token;
    }

    @Data
    public static class TaskDetail {
        private String status_uuid;
        private Long server_update_stamp;
    }

    @Data
    public static class TaskStatuses {
        private List<StatuDetail> task_statuses;
    }

    @Data
    public static class StatuDetail {
        private String uuid;
        private String name;
    }
}
